<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController{
    //添加用户
    public function add(){
        if(IS_POST){
            //提交数据
            $model = D('User');
            $data = $model -> create();
            //dump($data);die;
            if (!$data) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this -> error($model -> getError());
                exit();
            } else {
                $data['salt'] = uniqid();
                $data['password'] = md5($data['password'] . $data['salt']);
                $data['addtime'] = time();
                //dump($data);die;
                if ($model -> add($data)) {
                    $this -> success("用户添加成功", U('showList'));
                } else {
                    $this -> error("用户添加失败");
                }
            }
        }else{
            //显示表单
            $this -> display();
        }        
    }
    //用户列表
    public function showList(){
        //查询数据
        $model = M('User');
        $data = $model -> order('id desc') -> select();
        $this -> assign('data',$data);
        $this -> display();
    }
    //编辑用户
    public function edit(){
        if(IS_POST){
            //实例化模型类
            $model = D('User');
            $data = $model -> create();
            //dump($data);die;

            if(!$data){
                //提标用户验证失败
                //echo $model -> getError();die;
                //dump($model -> getError());die;
                $this -> error($model -> getError());exit;
            }

            //用户密码有提交
            if(isset($data['password'])){
                $data['salt'] = uniqid();
                $data['password'] = md5($data['password'] . $data['salt']);
            }
            $data['addtime'] = time();            
            //dump($data);die;

            $result = $model -> save($data);
            if($result){
                $this -> success('修改成功！',U('showList'));
                //$this -> redirect('Admin/Category/showList');
            }else{
                $this -> error('修改失败！');
            }
        }else{
            //接收id
            $id = I('get.id');
            //实例化模型
            $model = M('User');
            //查询类别信息
            $data = $model -> find($id);
            $this -> assign('data',$data);
            //展示模板
            $this -> display();
        }
    }
    //删除用户
    public function del(){
        if(IS_POST){
            //post删除
            //接收参数 
            $id = I('post.id');
            //模型实例化
            $model = M('User');
            //删除
            $result = $model -> delete($id);
            if($result){
                //成功
                $this -> success('删除成功！',U('showList'));
            }else{
                $this -> error('删除失败！');
            }
        }else{
            //接收id
            $id = I('get.id');
            //实例化模型
            $model = M('User');
            //查询类别信息
            $data = $model -> find($id);
            $this -> assign('data',$data);
            //展示模板
            $this -> display();
        }
    }
    //密码重置
    public function pw_reset(){
        if(IS_POST){
            //处理提交数据
            $post = I('post.');
            //dump($post);die;
            $model = M('User');
            $user = $model -> find(session('id'));
            $oldpwd = md5($post['password'] . $user['salt']);
            if($oldpwd == $user['password']){
                //echo 'ok';die;
                $newSalt = uniqid();
                $newpwd = md5($post['newpwd'] . $newSalt);
                $updata = array(
                    'id' => session('id'),
                    'password' => $newpwd,
                    'salt' => $newSalt,
                );

                $result = $model -> save($updata);
                if($result){
                    $this -> success('密码修改成功请重新登录！',U('Admin/public/logout'));
                }else{
                    $this -> error('密码修改失败！');
                }
            }else{
                $this -> error('原密码错误！');
            }
        }else{
            $this -> display();
        }
    }
}